#-*- coding: utf-8 -*-
#!/usr/bin/python
import paramiko

def ssh5(ip, port, username, passwd, cmd):
    print ip, port, username, passwd, cmd
    try:
        ssh = paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(ip, port, username, passwd, timeout=5)
        studin, stdout, stderr = ssh.exec_command(cmd)
        err = stderr.readlines()
        for e in err:
            print e
        out = stdout.readlines()
        for o in out:
            print o,
        print '%s\tOK\n' %(ip) 
        ssh.close()
        return 0
    except Exception,e:
        print e
        print '%s\tError\n' %(ip)
        return 1

if __name__ == '__main__':
    
#    cmd = 'echo hello'
#    username = "root"
#    password = "yuyao2014^&*"
#    host="60.12.222.185"
#    port = 20997
    host = raw_input("host: ")
    username = raw_input("username: ")
    password = raw_input("password: ")
    port = raw_input("port: ")
    cmd = raw_input("cmd: ")
    dict2 = {}
    dict2["host"] = host
    dict2["username"] = username
    dict2["password"] = password
    dict2["port"] = port
    dict2["cmd"] = cmd
#    dict2 = {"host": host, "username": username, "port": port, "password": password, "cmd": cmd}
#    print dict2["host"], dict2["port"], dict2["username"], dict2["password"], dict2["cmd"]
    ssh5(dict2["host"], int(dict2["port"]), dict2["username"], dict2["password"], dict2["cmd"])
#    print "Begin......"
#    ssh5(ip, port, username, passwd, cmd)
    
